package com.up.vms.domain.vm.repository;

import com.up.vms.domain.module.ActivityDetailVO;
import com.up.vms.domain.vm.entity.ActivityDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;

public interface ActivityDetailRepository extends JpaRepository<ActivityDetail, Long>, JpaSpecificationExecutor<ActivityDetail> {

    @Query(value= "select new com.up.vms.domain.module.ActivityDetailVO( " +
            " a.id, a.uploadTime, u.userName, u.userPhone, u.idNumber" +
            ") from ActivityDetail a left join UserDO u on a.userId = u.id " +
            " where a.status = 0 " +
            " and a.activityId = :activityId " +
            " and (:start is null or a.uploadTime >= :start) " +
            " and (:end is null or a.uploadTime < :end) " +
            " and (:userName is null or u.userName like %:userName%)")
    Page<ActivityDetailVO> getActivityDetails(@Param("activityId") Long activityId, @Param("start") Date startTime, @Param("end") Date endTime, @Param("userName") String userName, Pageable pageable);

    ActivityDetail findByActivityIdAndUserId(Long activityId, Long userId);
}
